package cn.wx.read.csv;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import org.apache.commons.lang.StringUtils;
import com.csvreader.CsvReader;
import cn.wx.common.utils.JSONUtils;
import cn.wx.common.utils.UUIDGenerator;

public class ReadExpertData {
	
	public static void main(String[] args) throws Exception {
		
		
		StringBuffer sb = null;
		
		/**
		 * 读取领域信息
		 */
		File file1 = new File("E:\\my_read_csv\\wfdata\\expert.csv");
        FileInputStream fileInputStream1 = new FileInputStream(file1);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\expert_new.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		.append(",").append(JSONUtils.c("nId_old"))
		.append(",").append(JSONUtils.c("name"))
		.append(",").append(JSONUtils.c("pnum"))
		.append(",").append(JSONUtils.c("bnum"))
		.append(",").append(JSONUtils.c("patNum"))
		.append(",").append(JSONUtils.c("patCit"))
		.append(",").append(JSONUtils.c("arrayFos"))
		.append(",").append(JSONUtils.c("curOrg"))
		.append(",").append(JSONUtils.c("language"))
		.append(",").append(JSONUtils.c("country"))
		;
		
		output.write(sb.toString());
		output.newLine();
		
		
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","name","pnum","bnum","patNum","patCit","arrayFos","curOrg","language","country"
			String nId = csvReader.get(0);
			String name = csvReader.get(1);
			String pnum = csvReader.get(2);
			String bnum = csvReader.get(3);
			String patNum = csvReader.get(4);
			String patCit = csvReader.get(5);
			String arrayFos = csvReader.get(6);
			String curOrg = csvReader.get(7);
			String language = csvReader.get(8);
			String country = csvReader.get(9);
			
			sb = new StringBuffer();
			sb.append(JSONUtils.c(UUIDGenerator.getRanLongUUID())).append(",")
			.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(name)).append(",")
			.append(JSONUtils.c(pnum)).append(",")
			.append(JSONUtils.c(bnum)).append(",")
			.append(JSONUtils.c(patNum)).append(",")
			.append(JSONUtils.c(patCit)).append(",")
			.append(JSONUtils.c(arrayFos)).append(",")
			.append(JSONUtils.c(curOrg)).append(",")
			.append(JSONUtils.c(language)).append(",")
			.append(JSONUtils.c(country));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			

        }
		
        output.flush();
        output.close();
        output = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
		
		
	}
	
}
